package com.cx.common.utils;

import com.cx.common.data.Pair;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Array;
import java.util.List;

/**
 * @Author:cxy
 * @Date:2019/10/24 15:11
 * 
 */
public class ArrayUtil {

	public static byte[] arrayByte(byte... args) {
		return args;
	}

	public static short[] arrayShort(short... args) {
		return args;
	}

	public static int[] arrayInt(int... args) {
		return args;
	}

	public static long[] arrayLong(long... args) {
		return args;
	}

	public static boolean[] arrayBoolean(boolean... args) {
		return args;
	}

	public static float[] arrayFloat(float... args) {
		return args;
	}

	public static double[] arrayDouble(double... args) {
		return args;
	}

	@SuppressWarnings("unchecked")
	public static <T> T[] array(T... args) {
		return args;
	}

	public static String[] addAll(String[] array1, String[] array2) {
		if (array1 == null) {
			return clone(array2);
		} else if (array2 == null) {
			return clone(array1);
		}
		String[] joinedArray = new String[array1.length + array2.length];
		System.arraycopy(array1, 0, joinedArray, 0, array1.length);
		System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
		return joinedArray;
	}

	public static String[] clone(String[] array) {
		if (array == null) {
			return null;
		}
		return (String[]) array.clone();
	}

	public static List<Pair<Integer, Integer>> arrayPair(Integer... args) {
		List<Pair<Integer, Integer>> pairs = New.arrayList(args.length / 2);
		for (int i = 0; i < args.length / 2; i++) {
			Pair<Integer, Integer> pair = new Pair<Integer, Integer>(args[i * 2], args[i * 2 + 1]);
			pairs.add(pair);
		}
		return pairs;
	}

	public static Integer[] arrayIntWithCapcity(int capcity) {
		Integer[] array = new Integer[capcity];
		return array;
	}
	
	public static Long[] arrayLongWithCapcity(int capcity) {
		Long[] array = new Long[capcity];
		return array;
	}

	public static String[] arrayStringWithCapcity(int capcity) {
		String[] array = new String[capcity];
		return array;
	}

	@SuppressWarnings("unchecked")
	public static <T> T arrayWithCapcity(Class<T> clazz, int capcity) {
		Object array = Array.newInstance(clazz, capcity);
		return (T) array;
	}

	@SuppressWarnings("unchecked")
	public static <T> T[] arrayForString(Class<T> clazz, String... args) {
		if (args == null || args.length == 0) {
			return (T[]) Array.newInstance(clazz, 0);
		}
		int length = args.length;
		T[] array = (T[]) Array.newInstance(clazz, length);
		for (int i = 0; i < length; i++) {
			array[i] = getValue(clazz, args[i]);
		}
		return array;
	}

	@SuppressWarnings("unchecked")
	public static <T> T getValue(Class<T> clazz, String str) {
		if (Integer.class.equals(clazz) || int.class.equals(clazz)) {
			return (T) Integer.valueOf(str);
		} else if (Double.class.equals(clazz) || double.class.equals(clazz)) {
			return (T) Double.valueOf(str);
		} else if (Float.class.equals(clazz) || float.class.equals(clazz)) {
			return (T) Float.valueOf(str);
		} else if (Long.class.equals(clazz) || long.class.equals(clazz)) {
			return (T) Long.valueOf(str);
		} else if (Boolean.class.equals(clazz) || boolean.class.equals(clazz)) {
			return (T) Boolean.valueOf(str);
		}
		return null;
	}

	public static Integer[] spiteByInt(String str, String splitable) {
		String[] arr = StringUtils.split(str, splitable);
		Integer[] array = arrayIntWithCapcity(arr.length);
		for (int i = 0; i < arr.length; i++) {
			String element = arr[i];
			Integer number = Integer.valueOf(element);
			array[i] = number;
		}
		return array;
	}
	
	public static Long[] spiteByLong(String str, String splitable) {
		String[] arr = StringUtils.split(str, splitable);
		Long[] array = arrayLongWithCapcity(arr.length);
		for (int i = 0; i < arr.length; i++) {
			String element = arr[i];
			Long number = Long.valueOf(element);
			array[i] = number;
		}
		return array;
	}

	public static <T> String joinArrayByComma(T[] array) {
		StringBuilder sb = StringTool.getStringBuilder();
		if (array != null) {
			int size = ArrayUtils.getLength(array);
			for (int i = 0; i < size; i++) {
				sb.append(CollectionsUtils.get(array, i)).append(Splitable.COMMA);
			}
			int length = sb.length();
			if (length > 0) {
				sb.setLength(length - 1);
			}
		}
		String value = sb.toString();
		StringTool.returnStringBuilder(sb);
		return value;
	}

}
